package com.jdpc.weparty.security

import com.jdpc.weparty.entity.User
import com.jdpc.weparty.reposity.UserRepository
import com.jdpc.weparty.service.udr.UdrService
import com.jdpc.weparty.service.user.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.stereotype.Service

@Service
class JwtUserDetailsServiceImpl : UserDetailsService {
    @Autowired
    private lateinit var userService: UserService

    @Autowired
    private lateinit var udrService: UdrService

    @Throws(UsernameNotFoundException::class)
    override fun loadUserByUsername(userAccount: String): UserDetails? {
        val user = userService.getUser(userAccount)
        if (user != null) {
            return JwtUserFactory.create(user, udrService.getRoles(user.id).toList())
        }
        return null
    }
}